package com.li.bbs;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ViewArticleServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ViewArticleServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Integer.valueOf(request.getParameter("id"));
		
		Article article = new Article();
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs  = null;
		
		try {
			conn = DB.getConn();
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select * from article where id =" + id );
			
			while(rs.next()){
				
				article.setId(rs.getInt("id"));
				article.setPid(rs.getInt("pid"));
				article.setRootid(rs.getInt("rootid"));
				article.setTitle(rs.getString("title"));
				article.setPdate(rs.getDate("pdate"));	
				article.setCont(rs.getString("cont"));
			
				
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		DB.free(rs, stmt, conn);
		
		request.setAttribute("article", article);
		
		this.getServletContext().getRequestDispatcher("/articleDetail.jsp").forward(request, response);
		

	
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
